package com.taotao.common.service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.message.BasicNameValuePair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

@Service
public class HttpClientService {

	@Autowired
	private HttpClient httpClient;
	
	private static final ObjectMapper MAPPER = new ObjectMapper();

	/**
	 * get请求，无参数
	 * 
	 * @param url
	 * @return
	 */
	public <T> T doGet(String url, TypeReference<T> type) {
		try {
			String jsonData = this.httpClient.execute(new HttpGet(url), new BasicResponseHandler());
			if(null == type){
				return (T)jsonData;
			}
			return MAPPER.readValue(jsonData, type);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * get请求，有参数
	 * 
	 * @param url
	 * @param params
	 * @return
	 */
	public <T> T doGetParam(String url, Map<String, String> params, TypeReference<T> type) {
		try {
			URIBuilder uriBuilder = new URIBuilder(url);
			for (Entry<String, String> entry : params.entrySet()) {
				uriBuilder.setParameter(entry.getKey(), entry.getValue());
			}
			String jsonData = this.httpClient.execute(new HttpGet(uriBuilder.build()), new BasicResponseHandler());
			if(null == type){
				return (T)jsonData;
			}
			return MAPPER.readValue(jsonData, type);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * post请求，无参
	 * 
	 * @param url
	 * @return
	 */
	public <T> T doPost(String url, TypeReference<T> type) {
		try {
			String jsonData = this.httpClient.execute(new HttpPost(url), new BasicResponseHandler());
			if(null == type){
				return (T)jsonData;
			}
			return MAPPER.readValue(jsonData, type);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * post请求，有参
	 * 
	 * @param url
	 * @return
	 */
	public <T> T doPostParam(String url, Map<String, String> params, TypeReference<T> type) {
		try {
			List<NameValuePair> parameters = new ArrayList<NameValuePair>(0);
			for (Entry<String, String> entry : params.entrySet()) {
				parameters.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
			}
			HttpPost httpPost = new HttpPost(url);
			// 将请求实体设置到httpPost对象中
			httpPost.setEntity(new UrlEncodedFormEntity(parameters));
			String jsonData = this.httpClient.execute(httpPost, new BasicResponseHandler());
			if(null == type){
				return (T)jsonData;
			}
			return MAPPER.readValue(jsonData, type);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * post请求，有参 json类型
	 * 
	 * @param url
	 * @return
	 */
	public <T> T doPostJson(String url, Object param, TypeReference<T> type) {
		try {
			HttpPost httpPost = new HttpPost(url);
			// 将请求实体设置到httpPost对象中
			httpPost.setEntity(new StringEntity(MAPPER.writeValueAsString(param), ContentType.APPLICATION_JSON));
			String jsonData = this.httpClient.execute(httpPost, new BasicResponseHandler());
			if(null == type){
				return (T)jsonData;
			}
			return MAPPER.readValue(jsonData, type);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

}
